<?php
namespace app\hotback\controller;
use think\Db;
class email extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 服务列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost()) {
            $params = input();
            if (!empty($params['page']) && !empty($params['limit'])) {
                $map = [
                    'delete_time' => 0,
                ];
                if (!empty($params['proxy_id'])) {
                    $map['proxy_id'] = ['in',[$params['proxy_id'],999]];
                }
                $list = Db::name('email_list')->where($map)->select();
                $count = Db::name('email_list')->where($map)->count('id');
//                $this->assign('list',$list);

                $result['code'] = 0;
                $result['msg'] = "请求成功";
                $result['data'] = $list;
                $result['count'] = $count;
                echo json_encode($result);

            }
        }else{
            $list = Db::name('proxy')->where('is_delete_time',0)->select();
            $this->assign('list',$list);
            return $this->fetch();
        }

	}
	
	
	public function index2()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [
					'p.is_delete_time' => 0,
					'p.type'=>2
				];
				if(!empty($params['company_name']))
				{
					$map['p.company_name'] = ['like','%'.$params['company_name'].'%'];
				}


				/*
                $count = Db::name('proxy')->where($map)->count();
				$list_data = Db::name('proxy')->where($map)->order('proxy_id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
                */

                $count = Db::table('jx_proxy')
                    ->alias('p')
                    ->join('area a','p.sheng = a.id')
                    ->join('area s','p.shi = s.id')
                    ->order('p.proxy_id desc')
                    ->field('p.proxy_id,p.user_name,p.company_name,p.add_time,p.qu as qustr,a.name as sheng,s.name as shi')
                    ->where($map)
                    ->count();

                $list_data = Db::table('jx_proxy')
                    ->alias('p')
                    ->join('area a','p.sheng = a.id')
                    ->join('area s','p.shi = s.id')
                    ->order('p.proxy_id desc')
                    ->limit(($params['page']-1)*$params['limit'],$params['limit'])
                    ->field('p.proxy_id,p.type,p.commission,p.user_name,p.company_name,p.add_time,p.qu as qustr,a.name as sheng,s.name as shi')
                    ->where($map)
                    ->select();

                if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						//编辑url
						$list_data[$key]['save_url'] = url('proxy/saveinfo', ['proxy_id' => $value['proxy_id'],'type'=>2]);
                        $list_data[$key]['add_time'] = date("Y-m-d H:i:s",$value['add_time']);

                        switch ($value['type']) {
                            case 1:
                                $list_data[$key]['typename'] = '分公司';
                                 break;
                            case 2:
                                $list_data[$key]['typename'] = '加盟商';
                                break;
                            default:
                                $list_data[$key]['typename'] = '无';
                        }

                        $qustr = $value['qustr'];

                        $qulists = Db::name('Area')->where('id','in',$qustr)->column('name');

                        if(!empty($qulists)) {
                            $qu_str = '';
                            foreach ($qulists AS $k=>$v) {
                                $qu_str.=$v.",";
                            }
                            $qu_str = substr($qu_str,0,strlen($qu_str)-1);
                            $list_data[$key]['qu'] = $qu_str;
                        }

					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
		    $day=db::name('xunh')->where('proxy_id',0)->value('day');
		    
		    $this->assign('day',$day);
		    
			return $this->fetch('');
		}
	}

	/**
	 * 服添添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();

		if(!empty($params['id']))
		{
            //查询
			$info = Db::name('email_list')->where(['id' => $params['id']])->find();
            $proxy = Db::name('proxy')->where('is_delete_time',0)->select();
            $info['proxy'] = $proxy;
            $this->assign('info',$info);

		}else{
            $proxy = Db::name('proxy')->where('is_delete_time',0)->select();
            $info['proxy'] = $proxy;
            $this->assign('info',$info);
        }

		return $this->fetch();
	}

    public function area() {
        $params = input();
        $id = $params['id'];
        $area = Db::name('Area')->where(['pid' => $id,'is_enable'=>'1'])->select();

        $result = array();
        if($area) {
            $result['code'] = 1;
            $result['msg'] = "请求成功";
            $result['data'] = $area;

        }else {
            $result['code'] = 0;
            $result['msg'] = "请求失败";
        }

        return json($result);
    }

    public function area_qu() {
        $params = input();
        $id = $params['id'];
        $proxy_id = isset($params['proxy_id']) ?  $params['proxy_id'] : 0;

        if($id == '0') {
            $result['code'] = 0;
            $result['msg'] = "请选择市";
            return json($result);
        }

        $area = Db::name('Area')->where(['pid' => $id,'is_enable'=>'1'])->select();
        $html = '';


        $qu = [];
        if($proxy_id) {
            $proxy_info = Db::name('Proxy')->where("proxy_id = '$proxy_id' ")->find();
            if(!empty($proxy_info['qu'])) {
                $qu = explode(',',$proxy_info['qu']);
            }
        }

        foreach ($area AS $k=>$v) {
            $id = $v['id'];
            $res = Db::name('Proxy')->where("FIND_IN_SET($id,qu) AND proxy_id != '$proxy_id' ")->find();
            if($res) {
                $html  .='<input type="checkbox" name="qu['.$v['id'].']" lay-skin="primary" title="'.$v['name'].'"  disabled=""  >';
            }else {
                if(in_array($v['id'],$qu)) {
                    $html  .='<input type="checkbox" name="qu['.$v['id'].']" lay-skin="primary" title="'.$v['name'].'"  checked=""  >';
                }else {
                    $html  .='<input type="checkbox" name="qu['.$v['id'].']" lay-skin="primary" title="'.$v['name'].'"   >';
                }
            }
        }

        $result = array();
        if($area) {
            $result['code'] = 1;
            $result['msg'] = "请求成功";
            $result['data'] = $html;

        }else {
            $result['code'] = 0;
            $result['msg'] = "请求失败";
        }

        return json($result);
    }


	/**
	 * 服务添加/修改
	 */
	public function save()
	{
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        $params = input();

        $validate = $this->validate(
            $params,
            [
                'email' => 'require',

            ],
            [
                'email.require' => '请输入邮箱',
            ]
        );
        if (true !== $validate) {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }

        if (isset($params['proxy_id'])) {
            $proxy = Db::name('proxy')->where('proxy_id', $params['proxy_id'])->find();
        }

        //基础数据
        $data = [
            'email' => $params['email'],
            'proxy_id' => $params['proxy_id'],
            'act_city' => !empty($proxy['company_name']) ? $proxy['company_name'] : 'all',
            'status' => $params['status'],
        ];
        if (!empty($params['e_id'])){
            $result = Db::name('email_list')->where('id',$params['e_id'])->update($data);

        }else{
            $result = Db::name('email_list')->insert($data);
        }
        if ($result) {
            $retrun['code'] = 0;
            $retrun['msg'] = '操作成功';

        }else{
            $retrun['code'] = -1;
            $retrun['msg'] = '操作失败';
        }
        echo json_encode($retrun);
	}

	/**
	 * 服务删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$data = [
				'delete_time' => time(),
			];
			if(Db::name('email_list')->where(['id' => intval($params['id'])])->update($data))
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
}